<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <FormItem name="loginCode" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.loginCode"
          :placeholder="t('sys.login.userName')"
        />
      </FormItem>

      <FormItem name="password" class="enter-x">
        <!-- <Input size="large" v-model:value="formData.password" :placeholder="t('请输入新密码')" /> -->
        <InputPassword
          size="large"
          visibilityToggle
          v-model:value="formData.password"
          :placeholder="t('请输入新密码')"
          autocomplete="false"
        />
      </FormItem>
      <FormItem name="fpValidCode" class="enter-x">
        <CountdownInput
          size="large"
          v-model:value="formData.fpValidCode"
          :placeholder="t('sys.login.smsCode')"
          :sendCodeApi="sendCodeApi"
        />
      </FormItem>
      <!-- <FormItem name="sms" class="enter-x flex">
        <div class="flex items-center justify-between">
          <BasicDragVerify ref="el1"  @success="handleSuccess" />
        <a-button  class="ml-2" @click="handleBtnClick(el1)"> 还原 </a-button>
        </div>
      </FormItem> -->
      <!-- <div class="flex justify-center p-4 items-center ">
        <BasicDragVerify ref="el1" @success="handleSuccess" />
        <a-button type="primary" class="ml-2" @click="handleBtnClick(el1)"> 还原 </a-button>
      </div> -->

      <FormItem class="enter-x">
        <Button type="primary" size="large" block @click="handleReset" :loading="loading">
          {{ t('common.resetText') }}
        </Button>
        <Button size="large" block class="mt-4" @click="handleBackLogin">
          {{ t('sys.login.backSignIn') }}
        </Button>
      </FormItem>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, computed, unref } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button } from 'ant-design-vue';
  import { CountdownInput } from '/@/components/CountDown';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useLoginState, useFormRules, LoginStateEnum } from './useLogin';
  import { BasicDragVerify, DragVerifyActionType, PassingData } from '/@/components/Verify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getFpValidCode, savePwdByValidCode } from '/@/api/sys/login';
  import { encryptByBase64 } from '/@/utils/cipher';

  const InputPassword = Input.Password;
 
  const { createMessage,showMessage } = useMessage();
  const FormItem = Form.Item;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState } = useLoginState();
  const { getFormRules } = useFormRules();

  const formRef = ref();
  const loading = ref(false);

  const formData = reactive({
    loginCode: '',
    password: '',
    // validCode: '9999',
    fpValidCode: '',
  });

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD);

  async function handleReset() {
   const form = unref(formRef);
   if (!form) return;
    // await form.resetFields();
   let res = await savePwdByValidCode({
        loginCode: formData.loginCode,
				fpValidCode: formData.fpValidCode,
				password: encryptByBase64(formData.password)
   })
   showMessage(res.message);
   if (res.result == 'true') {
     handleBackLogin()
		}
  }
  async function formValid() {
			if (!formData.loginCode) {
				showMessage('请输入账号');
				return false;
			}
			if (!formData.password) {
				showMessage('请输入新密码');
				return false;
			}
			return true;
	}

  async function sendCodeApi(){
    // if (formValid()) {
		// 		return;
		// }
    if(!formData.loginCode){
      showMessage('请输入账号');
    }else if(!formData.password){
      showMessage('请输入新密码');
    }else{
      getFpValidCode({
        loginCode: formData.loginCode,
        validCode:'9999',
        validType: 'mobile'
      }).then(res=>{
        showMessage(res.message);
      })
      return true
    }
  }

  // const el1 = ref<Nullable<DragVerifyActionType>>(null);

  // function handleSuccess(data: PassingData) {
  //   const { time } = data;
  //   createMessage.success(`校验成功,耗时${time}秒`);
  // }

  // function handleBtnClick(elRef: Nullable<DragVerifyActionType>) {
  //   if (!elRef) {
  //     return;
  //   }
  //   elRef.resume();
  // }


</script>
